package com.wlzg.collectiontool.view

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.dfqin.grantor.PermissionListener
import com.github.dfqin.grantor.PermissionsUtil

open class BaseActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    protected open fun openActivity(pClass: Class<*>?) {
        openActivity(pClass, null)
    }

    protected open fun openActivity(pClass: Class<*>?, pBundle: Bundle?) {
        val intent = Intent(this, pClass)
        if (pBundle != null) {
            intent.putExtras(pBundle)
        }
        startActivity(intent)
    }

    fun requestPermission(
        activity: Activity?,
        per: Array<String>,
        permissionLisenter: PermissionListener?
    ) {
        if (PermissionsUtil.hasPermission(activity!!, *per)) {
            //有访问权限
            permissionLisenter?.permissionGranted(per)
        } else {
            PermissionsUtil.requestPermission(activity, object : PermissionListener {
                override fun permissionGranted(permissions: Array<String>) {
                    //用户授予了权限
                    if (permissionLisenter != null) {
                        permissionLisenter.permissionGranted(permissions)
                    }
                }

                override fun permissionDenied(permissions: Array<String>) {
                    //用户拒绝了申请
                    if (permissionLisenter != null) {
                        permissionLisenter.permissionDenied(permissions)
                    }
                }
            }, *per)
        }
    }
}